package com.joymart.config;

import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.error.handling.JoymartInternalException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(JoymartBusinessException.class)
    public ResponseEntity<?> handleBusinessException(JoymartBusinessException ex) {
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body(Map.of("error", "Bad Request", "message", ex.getMessage()));
    }

    @ExceptionHandler(JoymartInternalException.class)
    public ResponseEntity<?> handleInternalException(JoymartInternalException ex) {
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(Map.of("error", "INTERNAL_SERVER_ERROR", "message", ex.getMessage()));
    }
}
